package com.xiaoyu.lanling.feature.visitor.data

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.base.utils.JsonUtil
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.visitor.FootprintListEvent
import com.xiaoyu.lanling.feature.visitor.model.VisitorItem

class FootprintListViewData() : PagedListDataModelWithRequestTag<VisitorItem, Any>() {

    private val requestTag = Any()

    override fun doQueryData() {
        RequestCreator.create(FootprintListEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_GET_MY_FOOT_PRINT)
                .appendQueryDataTo(listPageInfo)
                .setRequestHandler(object : RequestDefaultHandler<FootprintListEvent, JsonData>() {

                    override fun processOriginData(originData: JsonData): FootprintListEvent {
                        val rawData = originData.optJson("data")
                        val list = JsonUtil.convertToList(rawData.optJson("list")) { rawItem -> VisitorItem(rawItem) }
                        updateResult(rawData, list)
                        return FootprintListEvent(requestTag)
                    }

                    override fun onRequestFinish(event: FootprintListEvent) {
                        event.post()
                    }
                })
                .enqueue()
    }

    override fun getRequestTag(): Any {
        return requestTag
    }
}